import { Component, OnInit } from '@angular/core';
import { RoleRight } from 'src/kalend';
import { NzMessageService } from 'ng-zorro-antd';
import { CityService } from 'src/app/service/city.service';
import { InvitationawardService } from 'src/app/service/invitationaward.service';

@Component({
  selector: 'app-invitationaward',
  templateUrl: './invitationaward.component.html',
  styleUrls: ['./invitationaward.component.scss'],
  providers: [CityService, InvitationawardService]
})
export class InvitationawardComponent implements OnInit {
  user: any = RoleRight.getStorage();
  title: any;
  pageIndex = 1;
  pageSize = 15;
  total = 1;
  dataSet = [];
  loading = true;
  isOkLoading = false;
  isVisible = false;
  doc: any = {};
  status: any;
  statuslist = [{ key: null, value: '全部' }, { key: 1, value: '有效' }, { key: 0, value: '禁用' }];
  addareas: Array<any> = [];
  areas: Array<any> = [];
  nzOptions: Array<any> = [];
  constructor(private service: InvitationawardService, private msg: NzMessageService, private cityservice: CityService) {
    this.cityservice.querys().then((res) => {
      this.nzOptions = res;
    });
  }

  ngOnInit() {
    this.searchData();
  }

  searchData(reset: boolean = false): void {
    this.loading = true;
    this.service.query({
      offset: this.pageIndex,
      limit: this.pageSize,
      areas: this.areas,
      title: this.title,
      status: this.status
    }).then((res) => {
      this.loading = false;
      this.total = res.total;
      this.dataSet = res.rows;
    });
  }
  /**新增 */
  add() {
    this.doc.createuser = this.user._id;
    this.isVisible = true;
  }

  /**修改 */
  edit(id: any) {
    this.service.queryById(id).then((res) => {
      this.addareas = [];
      if (res.province && res.province !== '') {
        this.addareas.push(res.province)
      }
      if (res.city && res.city !== '') {
        this.addareas.push(res.city)
      }
      if (res.district && res.district !== '') {
        this.addareas.push(res.district)
      }
      this.doc = res;
      this.isVisible = true;
    });
  }
  /**禁用 */
  lock(id: any) {
    this.service.lock(id).then((res) => {
      this.searchData();
    });
  }
  /**启用 */
  open(id: any) {
    this.service.open(id).then((res) => {
      this.searchData();
    });
  }

  handleOk(): void {
    this.isOkLoading = true;
    if (this.areas.length <= 0) {
      this.msg.warning('请选择城市');
    } else if (!this.doc.time) {
      this.msg.warning('请填写奖励时限');
    } else if (this.doc.fnum && !this.isIntNum(this.doc.fnum)) {
      this.msg.warning('请正确设置第一批邀请人数');
    } else if (this.doc.fmoney && !this.isIntNum(this.doc.fmoney)) {
      this.msg.warning('请正确设置第一批邀请奖励金额');
    } else if (this.doc.snum && !this.isIntNum(this.doc.snum)) {
      this.msg.warning('请正确设置第二批邀请人数');
    } else if (this.doc.smoney && !this.isIntNum(this.doc.smoney)) {
      this.msg.warning('请正确设置第二批邀请奖励金额');
    } else if (this.doc.tnum && !this.isIntNum(this.doc.tnum)) {
      this.msg.warning('请正确设置第三批邀请人数');
    } else if (this.doc.tmoney && !this.isIntNum(this.doc.tmoney)) {
      this.msg.warning('请正确设置第三批邀请奖励金额');
    } else {
      if (this.addareas.length === 1) {
        this.doc.province = this.addareas[0];
      }
      if (this.addareas.length === 2) {
        this.doc.citytype = this.addareas.length;
        this.doc.province = this.addareas[0];
        this.doc.city = this.addareas[1];
      }
      if (this.addareas.length === 3) {
        this.doc.citytype = this.addareas.length;
        this.doc.province = this.addareas[0];
        this.doc.city = this.addareas[1];
        this.doc.district = this.addareas[2];
      }
      this.service.save(this.doc).then((res) => {
        this.searchData();
        this.msg.success('保存成功！');
        this.isVisible = false;
        this.doc = {};
      });
    }
  }
  handleCancel(): void {
    this.isVisible = false;
    this.doc = {};
  }
  /**
* 校验正负正数就返回true
**/

  isIntNum(val) {
    if (parseFloat(val).toString() === 'NaN' || parseFloat(val) < 0) {
      return false;
    } else {
      return true;
    }
  }
  onChanges(values: any): void {
    console.log(values, this.areas);
  }
}
